package ymy.common;

import ymy.GeneralConfig;
import java.util.List;

public class Utility {

    public static boolean doubleEqual(double d1, double d2) {
        if (Math.abs(d1 - d2) <= 1e-6)
            return true;
        return false;
    }

    public static boolean doubleGreater(double d1, double d2) {
        if (d1 - d2 > 1e-6)
            return true;

        return false;
    }

    public static boolean doubleLess(double d1, double d2) {
        if (d2 - d1 > 1e-6)
            return true;

        return false;
    }

    public static Coordinate copyOfCoordinate(Coordinate orig) {
        return new Coordinate(orig.getX(), orig.getY());
    }

    public static double distance(Coordinate c1, Coordinate c2) {
        double val = Math.pow(c1.getX() - c2.getX(), 2) + Math.pow(c1.getY() - c2.getY(), 2);
        if (val < 1.00) val = 1.00;
        return Math.sqrt(val);
    }

    public static String stringDirection(int i) {
        if (i == GeneralConfig.WEST) return "WEST";
        if (i == GeneralConfig.EAST) return "EAST";
        if (i == GeneralConfig.SOUTH) return "SOUTH";
        if (i == GeneralConfig.NORTH) return "NORTH";

        return "NULL";
    }

    private static double determinant(double v1, double v2, double v3, double v4) {
        return v1 * v4 - v2 * v3;
    }

    private static boolean lineIntersect(Coordinate a, Coordinate b, Coordinate c, Coordinate d) {
        double xa = a.getX();
        double ya = a.getY();
        double xb = b.getX();
        double yb = b.getY();
        double xc = c.getX();
        double yc = c.getY();
        double xd = d.getX();
        double yd = d.getY();
        double delta = determinant(xb-xa, xc-xd, yb-ya, yc-yd);

        if (delta <= 1e-6 && delta >= -(1e-6))
            return false;

        double nda = determinant(xc-xa, xc-xd, yc-ya, yd-yc) / delta;
        if (nda > 1 || nda < 1e-6)
            return false;

        double miu = determinant(xb-xa, xc-xa, yb-ya, yc-ya) / delta;
        if (miu > 1 || miu < 1e-6)
            return false;

        return true;
    }

    private static boolean linkIntersect(Coordinate lstart, Coordinate lend, Building building) {

        Coordinate bx1 = building.getStartPosition();
        Coordinate bx2 = new Coordinate(bx1.getX() + building.getHorizontalLength(), bx1.getY());
        Coordinate bx3 = new Coordinate(bx1.getX(), bx1.getY() + building.getVerticalLength());
        Coordinate bx4 = new Coordinate(bx1.getX() + building.getHorizontalLength(), bx1.getY() + building.getVerticalLength());

        boolean flag1 = lineIntersect(lstart, lend, bx1, bx2);
        boolean flag2 = lineIntersect(lstart, lend, bx1, bx3);
        boolean flag3 = lineIntersect(lstart, lend, bx3, bx4);
        boolean flag4 = lineIntersect(lstart, lend, bx2, bx4);

        boolean flag5 = false;
        if (lstart.getX() >= bx1.getX() && lstart.getX() <= bx2.getX()
        && lend.getX() >= bx1.getY() && lend.getY() <= bx3.getY())
            flag5 = true;

        return (flag1 || flag2 || flag3 || flag4 || flag5);
    }

    public static boolean linkSheltered(Coordinate cs, Coordinate ce, List<Building> blists) {
        for (Building b : blists) {
            if (linkIntersect(cs, ce, b))
                return true;
        }

        return false;
    }

}
